﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class LoadFromFile : MonoBehaviour {

    private string FilePath;
	// Use this for initialization
	void Start () {
        FilePath = Application.streamingAssetsPath + "/AB/scene/wall.unity3d";
        //AssetBundle ab1 = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/AB/share.unity3d");//有依赖包的情况先加载依赖包才能完全加载
        AssetBundle ab = AssetBundle.LoadFromFile(FilePath);
        /* 使用方式 根据情况使用
         *  1，AssetBundle.LoadFromMemoryAsync （从内存加载）参数为字节数组，将ab包转为字节流读取
            2，AssetBundle.LoadFromFile （专门处理从本地加载）
            3，WWW.LoadFromCacheOrDownload （2017逐渐弃用）
            4，UnityWebRequest （专门处理从服务器加载）

         */

        /*
        //第一种加载AB的方式 LoadFromMemoryAsync
        //AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        //yield return request;
        //AssetBundle ab = request.assetBundle;
        //AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));


        //第二种加载AB的方式 LoadFromFile
        //AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        //yield return request;
        //AssetBundle ab = request.assetBundle;

        //第三种加载AB的方式 WWW
        //while(Caching.ready == false)
        //{
        //    yield return null;
        //}
        
        ////WWW www = WWW.LoadFromCacheOrDownload(@"file://E:\Unity Project Workspace\AssetBundleProject\AssetBundles\cubewall.unity3d", 1);本地 file://  file:/// 前缀
        //WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/cubewall.unity3d", 1);服务器
        //yield return www;
        //if (string.IsNullOrEmpty(www.error)==false )
        //{
        //    Debug.Log(www.error);yield break ;
        //}
        //AssetBundle ab = www.assetBundle;

        //第四种方式 使用UnityWebRequest
        //string uri = @"file://E:\Unity Project Workspace\AssetBundleProject\AssetBundles\cubewall.unity3d";
        //string uri = @"http://localhost/AssetBundles/cubewall.unity3d";
        //UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
        //yield return request.Send();
        //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
        //AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
         */

        //根据manifest加载依赖包
        AssetBundle manifestAB = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/AB/AB");
        AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        //foreach (string name in manifest.GetAllAssetBundles())
        //{
        //    Debug.Log(name);
        //}
        string[] strs = manifest.GetAllDependencies("scene/wall.unity3d");
        foreach (string name in strs)
        {
            Debug.Log(name);
            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/AB/" + name);
        }
        
        GameObject prefab = ab.LoadAsset<GameObject>("Cube");
        Instantiate(prefab);

	}

}
